package com.pantuo.sys.controller;

import com.pantuo.common.utils.TableRequest;
import com.pantuo.common.vo.JsonResult;
import com.pantuo.sys.service.UserService;
import com.pantuo.sys.vo.UserPermissionVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageImpl;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 用户登入
     *
     * @param map
     * @return
     */
    @RequestMapping("/doLogin")
    public JsonResult doLogin(@RequestParam Map<String, String> map) {
        Map<String, Object> userAndPer = userService.login(map);
        return new JsonResult(userAndPer);
    }


    /**
     * 添加用户
     *
     * @param userMap
     * @return
     */
    @RequestMapping("/doSaveObject")
    public JsonResult doSaveObject(@RequestParam Map<String, Object> userMap) {
        //userService.insertObject(userMap);
        return new JsonResult("save ok");
    }

    /**
     * 修改密码
     *
     * @param username_s
     * @param usedPwd_s
     * @param newPwd1_s
     * @param newPwd2_s
     * @return
     */
    @RequestMapping("/doUpdatePwd")
    public JsonResult doUpdatePwd(String username_s,
                                  String usedPwd_s,
                                  String newPwd1_s,
                                  String newPwd2_s) {
        userService.updatePwd(username_s, usedPwd_s, newPwd1_s, newPwd2_s);
        return new JsonResult("密码修改成功");
    }

    /**
     * 查询所有用户以及用户权限()
     *
     * @param request
     * @return
     */
    @RequestMapping("/doFindPageObjects")
    public JsonResult doFindPageObjects(TableRequest request) {
        PageImpl<UserPermissionVo> pageObjects = userService.findPageObjects(request);
        return new JsonResult(pageObjects);
    }

    @RequestMapping("/doDeleteObject")
    public JsonResult doDeleteObject(Integer id) {
        userService.deleteObject(id);
        return new JsonResult("delete ok");
    }

    @RequestMapping("/doUpdateUser")
    public JsonResult doUpdateUser(@RequestParam Map<String, Object> userMap) {
        System.out.println("userMap：" + userMap);
        userService.updateUser(userMap);
        return new JsonResult("update ok");
    }

    @RequestMapping("/doFindUserAndPer")
    public JsonResult doFindUserAndPer(Integer id) {
        return new JsonResult(userService.findUserAndPer(id));
    }

    @RequestMapping("/doReset")
    public JsonResult doResetPwd(Integer id) {
        userService.resetPwd(id);
        return new JsonResult("密码重置成功");
    }
}
